package com.jcfk.eam.controller.equ;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.security.utils.SecurityUtils;
import com.jcfk.common.web.domain.*;
import com.jcfk.eam.domain.dto.main.EamPointCheckDetailDTO;
import com.jcfk.eam.domain.dto.main.EamPointCheckOrderDTO;
import com.jcfk.eam.domain.query.main.EamPointCheckOrderQuery;
import com.jcfk.eam.enums.AssetKind;
import com.jcfk.eam.enums.TaskKind;
import com.jcfk.eam.service.main.EamPointCheckOrderService;
import com.jcfk.system.api.utils.CodeBuildUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备巡检工单
 */
@Api(tags = {"设备巡检工单"})
@RestController
@RequestMapping("equRouteInspectionOrder")
public class EquRouteInspectionOrderController {

    @Autowired
    private EamPointCheckOrderService eamPointCheckOrderService;

    /**
     * 分页查询设备巡检工单
     */
    @ApiOperation("分页查询设备巡检工单")
    @PostMapping("/queryByPage")
    public RetPage<EamPointCheckOrderDTO> queryByPage(@RequestBody ReqPageParam<EamPointCheckOrderQuery> requestParam) {
        //设置默认排序
        if(requestParam.getSort().getOrderItem()==null){
            ReqSortParam<EamPointCheckOrderQuery> sortParam=new ReqSortParam();
            sortParam.setColumn("lastUpdateTime");
            sortParam.setOrderBy("desc");
            requestParam.setSort(sortParam);
        }
        DataPage<EamPointCheckOrderDTO> page = new DataPage<EamPointCheckOrderDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
                EamPointCheckOrderDTO.class);

        requestParam.getData().setTaskKind(TaskKind.ROUTE_INSPECTION.getValue());
        requestParam.getData().setAssetKind(AssetKind.PRODUCTION_EQUIPMENT.getValue());
        requestParam.getData().setAssetCode(requestParam.getData().getSearchContent());

        page = eamPointCheckOrderService.queryByPlan(page, requestParam.getData());

        return new RetPage<>(page);
    }

    /**
     * 根据任务分页查询设备巡检工单
     */
    @ApiOperation("根据任务分页查询设备巡检工单")
    @PostMapping("/queryByTask")
    public RetPage<EamPointCheckOrderDTO> queryByTask(@RequestBody ReqPageParam<EamPointCheckOrderQuery> requestParam) {
        if(StrUtil.isEmpty(requestParam.getData().getInspectionTaskId())){
            return new RetPage<>(new DataPage<EamPointCheckOrderDTO>((long) requestParam.getCurrent(),
                    (long) requestParam.getSize(),
                    requestParam.getSort().getOrderItem(),
                    EamPointCheckOrderDTO.class));
        }
        //设置默认排序
        if(requestParam.getSort().getOrderItem()==null){
            ReqSortParam<EamPointCheckOrderQuery> sortParam=new ReqSortParam();
            sortParam.setColumn("assetCode");
            sortParam.setOrderBy("asc");
            requestParam.setSort(sortParam);
        }
        DataPage<EamPointCheckOrderDTO> page = new DataPage<EamPointCheckOrderDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
                EamPointCheckOrderDTO.class);
        page = eamPointCheckOrderService.queryByTask(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     * 分页查询我的设备巡检工单
     */
    @ApiOperation("分页查询我的设备巡检工单")
    @PostMapping("/queryMyList")
    public RetPage<EamPointCheckOrderDTO> queryMyList(@RequestBody ReqPageParam<EamPointCheckOrderQuery> requestParam) {
        //设置默认排序
        if(requestParam.getSort().getOrderItem()==null){
            ReqSortParam<EamPointCheckOrderQuery> sortParam=new ReqSortParam();
            sortParam.setColumn("lastUpdateTime");
            sortParam.setOrderBy("desc");
            requestParam.setSort(sortParam);
        }
        requestParam.getData().setExecutePersonId(SecurityUtils.getUserId());
        DataPage<EamPointCheckOrderDTO> page = new DataPage<EamPointCheckOrderDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
                EamPointCheckOrderDTO.class);

        requestParam.getData().setTaskKind(TaskKind.ROUTE_INSPECTION.getValue());
        requestParam.getData().setAssetKind(AssetKind.PRODUCTION_EQUIPMENT.getValue());
        requestParam.getData().setAssetCode(requestParam.getData().getSearchContent());

        page = eamPointCheckOrderService.queryMyList(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     * 获取设备巡检工单
     * 根据id 查询详情
     */
    @ApiOperation("查询设备巡检工单")
    @GetMapping("/get/{id}")
    public RetResult<EamPointCheckOrderDTO> get(@PathVariable("id") String id) {
        EamPointCheckOrderDTO item = eamPointCheckOrderService.getOrder(id);
        return RetResult.ok(item);
    }

    /**
     * 获取巡检单明细
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取巡检单明细", notes = "获取巡检单明细", httpMethod = "POST")
    @PostMapping("getDetails")
    public RetResult<List<EamPointCheckDetailDTO>> getDetails(@RequestBody EamPointCheckOrderQuery query) {

        List<EamPointCheckDetailDTO> list = eamPointCheckOrderService.getDetails(query);

        return RetResult.ok(list);
    }

    /**
     * 保存设备巡检工单
     */
    @ApiOperation("保存设备巡检工单")
    @RequestLogHeader(modular = "设备巡检工单", businessType = "保存")
    @PostMapping("save")
    public RetResult<?> save(@RequestBody EamPointCheckOrderDTO eamPointCheckOrderDTO) {
        eamPointCheckOrderDTO.setAssetKind(AssetKind.PRODUCTION_EQUIPMENT.getValue());
        eamPointCheckOrderDTO.setOrderStatus(1);
        eamPointCheckOrderService.saveOrder(eamPointCheckOrderDTO);
        return RetResult.ok();
    }

    /**
     * 快速提交巡检通过
     * @param eamPointCheckOrderDTO
     * @return
     */
    @RequestLogHeader(modular = "设备巡检工单", businessType = "快速提交巡检通过")
    @PostMapping("submitNormal")
    public RetResult<?> submitNormal(@RequestBody EamPointCheckOrderDTO eamPointCheckOrderDTO){
        eamPointCheckOrderDTO.setAssetKind(AssetKind.PRODUCTION_EQUIPMENT.getValue());
        eamPointCheckOrderService.submitNormal(eamPointCheckOrderDTO);
        return RetResult.ok();
    }

    /**
     * 提交设备巡检工单
     */
    @ApiOperation("保存设备巡检工单")
    @RequestLogHeader(modular = "设备巡检工单", businessType = "提交")
    @PostMapping("submit")
    public RetResult<?> submit(@RequestBody EamPointCheckOrderDTO eamPointCheckOrderDTO) {
        eamPointCheckOrderDTO.setAssetKind(AssetKind.PRODUCTION_EQUIPMENT.getValue());
        eamPointCheckOrderService.submit(eamPointCheckOrderDTO);
        return RetResult.ok();
    }
}
